package com.goldplusgold.tradedelay.app.my.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.goldplusgold.live.LVDataManager;
import com.goldplusgold.live.bean.LikeBean;
import com.goldplusgold.live.bean.QuestionBean;
import com.goldplusgold.live.result.ResultLikes;
import com.goldplusgold.support.lib.base.activity.BaseActivity;
import com.goldplusgold.support.lib.widget.TitleView;
import com.goldplusgold.tradedelay.app.R;
import com.goldplusgold.tradedelay.app.home.live.ColumnInfoActivity;
import com.goldplusgold.tradedelay.app.home.live.LiveDetailActivity;
import com.goldplusgold.tradedelay.app.home.live.base.LiveBaseCallBack;
import com.goldplusgold.tradedelay.app.my.adapter.MyFollowAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;




public class MyFollowActivity extends BaseActivity {
    @BindView(R.id.title_view)
    TitleView titleView ;
    @BindView(R.id.my_follow_rv)
    RecyclerView my_follow_rv ;
    @BindView(R.id.hint_layout)
    LinearLayout hint_layout ;

    private MyFollowAdapter adapter ;
    private List<LikeBean> lists;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lists = new ArrayList<LikeBean>();
        initData();
        initAdapter();
    }



    @Override
    public int getLayoutId() {
        return R.layout.activity_my_follow;
    }

    @Override
    protected void initView() {
        super.initView();
        titleView.getTitletextView().setVisibility(View.VISIBLE);
        titleView.getBackView().setVisibility(View.VISIBLE);


        titleView.getTitletextView().setText("我的关注");
        titleView.getBackView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    private void initData() {
        showLoadingControl();
        LVDataManager.getInstance().getFollows(new LiveBaseCallBack<ResultLikes>(this){

            @Override
            public void onResponse(Call<ResultLikes> call, Response<ResultLikes> response) {
                dismisLoadingControl();
                super.onResponse(call , response);
                if(response.isSuccessful()){
                    if(response.body() != null){
                        lists.clear();
                        List<LikeBean> beans = response.body().getLikes();
                        if(beans != null && beans.size() != 0){
                            lists.addAll(beans);
                            hint_layout.setVisibility(View.GONE);
                            adapter.notifyDataSetChanged();
                        }else{
                            hint_layout.setVisibility(View.VISIBLE);
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<ResultLikes> call, Throwable t) {
                dismisLoadingControl();
                showToastMessage("服务器断片了~~" , Toast.LENGTH_SHORT);
            }
        });

    }

    private void initAdapter() {
        adapter = new MyFollowAdapter(this, lists, new MyFollowAdapter.MyFollowAdapterListener() {
            @Override
            public void clickCancel(String columnID) {
               // showToastMessage(columnID , Toast.LENGTH_SHORT);
                LVDataManager.getInstance().unFollow(columnID, new LiveBaseCallBack<QuestionBean>(MyFollowActivity.this) {

                    @Override
                    public void onResponse(Call<QuestionBean> call, Response<QuestionBean> response) {
                        super.onResponse(call , response);
                        if(response.isSuccessful()){
                            if(response.body() != null){
                                boolean flag = response.body().getStatus() ;
                                if(flag == true){
                                    initData();
                                }else{
                                    showToastMessage(response.body().getInfo()  ,Toast.LENGTH_SHORT);
                                }
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<QuestionBean> call, Throwable t) {

                    }
                });
            }

            @Override
            public void clickItem(int pos) {
                /*lists.get(pos).getColumnIcon()*/

                Intent intent = new Intent(MyFollowActivity.this , /*ColumnIntroActivity.class*/ColumnInfoActivity.class);
                intent.putExtra("columnID",lists.get(pos).getColumnID());
                startActivity(intent);
            }


        });
        my_follow_rv.setLayoutManager(new LinearLayoutManager(this));
        my_follow_rv.setAdapter(adapter);
    }
}
